package StudyCode.summary.aclfile;

import java.io.*;
import java.util.Vector;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ReadFileWithRandomAccessFile {
	private Vector<Integer> victor;
	private static final int SIZE = 10;

	public ReadFileWithRandomAccessFile() {
		victor = new Vector<Integer>(SIZE);
		for (int i = 0; i < SIZE; i++)
			victor.addElement(new Integer(i));

		this.readList("E:/Copy/1.txt");
		this.writeList();
	}

	private void readList(String fileName) {
		String line = null;
		try {
			RandomAccessFile raf = new RandomAccessFile(fileName, "r");
			while ((line = raf.readLine()) != null) {
				try {
					Integer i = new Integer(Integer.parseInt(line));
					System.out.println(i);
					victor.addElement(i);
				} catch (Exception e) {
					continue;
				}
			}
		} catch (FileNotFoundException fnf) {
			System.err.println("文件: " + fileName + " 未找到.");
		} catch (IOException io) {
			System.err.println(io.toString());
		}
	}

	private void writeList() {
		PrintWriter out = null;

		try {
			out = new PrintWriter(new FileWriter("E:/Copy/2.txt"));

			for (int i = 0; i < victor.size(); i++)
				out.println("值: " + i + " = " + victor.elementAt(i));
		} catch (ArrayIndexOutOfBoundsException e) {
			System.err.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage());
		} catch (IOException e) {
			System.err.println("Caught IOException: " + e.getMessage());
		} finally {
			if (out != null) {
				System.out.println("关闭 PrintWriter");
				out.close();
			} else {
				System.out.println("PrintWriter 未打开");
			}
		}
	}

	public static void main(String[] args) {
		new ReadFileWithRandomAccessFile();
	}
}
